package com.clerkq.Scroller
{
	
	import com.library.core.IDisposable;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	import gs.TweenLite;

    public class ScrollerManager extends EventDispatcher implements IDisposable {

		public static const ON_SLIDE_UPDATE:String = "on_slide_update";
        private var _slider:DragBlock;
        private var _sliderRect:Rectangle;
        private var _content:DisplayObject
        private var _arrowPressed:Boolean;
        private var _slideDir:Number;
        private var _ticker:Timer;
        
        private var _containerSize:Number;	// 竖条则为Height，横条则为Width  
        private var _realWidth:Number;	// 实际图片的 "宽"
        private var _realHeight:Number;	// 实际图片的 "高"
        private var _basePos:Number;		// content 的初始位置，可以是X坐标，也可以是Y坐标
              
        private var _direction:String = ScrollerType.VERTICAL;

        public function ScrollerManager(	slider:DragBlock, 
        									sliderRect:Rectangle, 
        									content:DisplayObject, 
        									containerSize:Number, 
        									realWidth:Number,
        									realHeight:Number,
        									direction:String){
            _slider = slider;
            _sliderRect = sliderRect;
            _content = content;
            _containerSize = containerSize;
            _realWidth = realWidth;
            _realHeight = realHeight;
            _direction = direction;
            _basePos = (_direction == ScrollerType.VERTICAL) ? content.y : content.x;
            
            _ticker = new Timer(50);
            _ticker.addEventListener(TimerEvent.TIMER, update);
        }
        
        private function update(evt:TimerEvent):void{
            if (_arrowPressed){
            	if(_direction == ScrollerType.VERTICAL){
            		if ((_slider.y + _slideDir) > (_sliderRect.y + _sliderRect.height)){
	                    _slider.y = (_sliderRect.y + _sliderRect.height);
	                } else {
	                    if ((_slider.y + _slideDir) < _sliderRect.y){
	                        _slider.y = _sliderRect.y;
	                    } else {
	                        _slider.y = _slider.y + _slideDir;
	                    };
	                };
            	}else{
            		if ((_slider.x + _slideDir) > (_sliderRect.x + _sliderRect.width)){
	                    _slider.x = (_sliderRect.x + _sliderRect.width);
	                } else {
	                    if ((_slider.x + _slideDir) < _sliderRect.x){
	                        _slider.x = _sliderRect.x;
	                    } else {
	                        _slider.x = _slider.x + _slideDir;
	                    };
	                };
            	}
	                
            };
            
        	var slideDistance:Number;
            if(_direction == ScrollerType.VERTICAL){
            	slideDistance = _sliderRect.height;
            	var destY:Number = -1 * ((_slider.y - Arrows.HEIGHT) / slideDistance) * (_realHeight - _containerSize) + _basePos;
//            	Tweener.addTween(_content, {y:destY, time:0.18, onComplete:stopScroller, transition:"linear"});
				TweenLite.to(_content, 0.18, {y:destY, onComplete:stopScroller});
            }else{
            	slideDistance = _sliderRect.width;
            	var destX:Number = -1 * ((_slider.x - Arrows.WIDTH) / slideDistance)*(_realWidth - _containerSize) + _basePos;
//            	Tweener.addTween(_content, {x:destX, time:0.18, onComplete:stopScroller, transition:"linear"});
				TweenLite.to(_content, 0.18, {x:destX, onComplete:stopScroller});
            }
            this.dispatchEvent(new Event(ON_SLIDE_UPDATE,false, false));
        }
        
        private function stopScroller():void{
            _ticker.stop();
        }
        
        private function runScroller():void{
            _ticker.start();
        }
        
        // event /////////////////////////////////
        public function onPressArrow(evt:Event):void{
            if (evt.target.id == 0){
                _slideDir = -2;
            } else {
                _slideDir = 2;
            };
            _slideDir *= _direction == ScrollerType.VERTICAL ? 4 : 20;
            _arrowPressed = true;
            runScroller();
        }
        public function onReleaseArrow(evt:Event):void{
            _arrowPressed = false;
            stopScroller();
        }
        public function onPressSlider(evt:Event):void{
            runScroller();
        }
        public function onReleaseSlider(evt:Event):void{
            stopScroller();
        }
        
        // public ///////////////////////////
        public function setSize(dragRect:Rectangle, viewSize:Number, realSize:Number):void{
        	_sliderRect = dragRect;
        	_containerSize = viewSize;
        	if(_direction == ScrollerType.VERTICAL){
        		_realHeight = realSize;
            }else{
            	_realWidth = realSize;
            }
        }
        
        public function reset():void{
            _content.y = 0;
        }
        
        public function Dispose():void{
        	_ticker.removeEventListener(TimerEvent.TIMER, update);
        }
        
        
    }
}